package com.mofofa.maozy.components.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseDialogAction
import com.mofofa.maozy.databinding.DialogMaozyCityPickerVndBinding

class VndCityPickerDialog : DialogFragment(), BaseDialogAction {

    companion object {
        fun generateMaozyDialog(title: String?): VndCityPickerDialog {
            val dialog = VndCityPickerDialog()
            dialog.mMaozyTitle = title
            return dialog
        }
    }

    private lateinit var mMaozyViewMaozyBinding: DialogMaozyCityPickerVndBinding
    private lateinit var mPopListAdapter: PopListAdapter

    var mMaozyState: String = ""
    var mMaozyCity: String = ""
    var mMaozyPickMaozyType: Int = R.integer.PickerTypeState
    var mMaozyUIMaozyTag: String = ""

    private var mMaozyOnMaozyActionMaozyListener: (String, Int, PopListItem, Int) -> Unit =
        { uTag, pickTpe, listItem, index -> }
    private val mMaozyDataMaozyList =
        mutableListOf<PopListItem>() as ArrayList<PopListItem>
    private var mMaozyTitle: String? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);

        mMaozyViewMaozyBinding = DialogMaozyCityPickerVndBinding.inflate(layoutInflater)
        return mMaozyViewMaozyBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initMaozySubviews()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
        mMaozyViewMaozyBinding.textDialogCityTitle.text = mMaozyTitle
        mPopListAdapter.notifyDataSetChanged()
    }

    override fun initMaozySubviews() {
        mPopListAdapter =
            PopListAdapter(mMaozyDataMaozyList)

        mMaozyViewMaozyBinding.rcDialogCityList.layoutManager =
            LinearLayoutManager(context)
        mMaozyViewMaozyBinding.rcDialogCityList.adapter =
            mPopListAdapter

        mMaozyViewMaozyBinding.textDialogCityPickerState.text = mMaozyState
        mMaozyViewMaozyBinding.textDialogCityPickerCity.text = mMaozyCity

        if (mMaozyState.isBlank()) {
            mMaozyViewMaozyBinding.rlDialogCityPickerResultBg.visibility = View.GONE
        } else {
            mMaozyViewMaozyBinding.rlDialogCityPickerResultBg.visibility = View.VISIBLE
        }

        mMaozyViewMaozyBinding.textDialogCityPickerState.setOnClickListener {
            mMaozyPickMaozyType = R.integer.PickerTypeState
            mMaozyState = ""
            mMaozyCity = ""
            mMaozyViewMaozyBinding.textDialogCityPickerState.text = mMaozyState
            mMaozyViewMaozyBinding.textDialogCityPickerCity.text = mMaozyCity
            mMaozyViewMaozyBinding.rlDialogCityPickerResultBg.visibility = View.GONE
            mMaozyOnMaozyActionMaozyListener.invoke(
                mMaozyUIMaozyTag,
                R.integer.PickerTypeStateReload,
                PopListItem(mMaozyState, mMaozyState),
                -1
            )
        }

        mMaozyViewMaozyBinding.textDialogCityPickerCity.setOnClickListener {
            mMaozyPickMaozyType = R.integer.PickerTypeCity
            mMaozyViewMaozyBinding.textDialogCityPickerState.text = mMaozyState
            mMaozyViewMaozyBinding.textDialogCityPickerCity.text = mMaozyCity
            mMaozyViewMaozyBinding.rlDialogCityPickerResultBg.visibility = View.VISIBLE
            mMaozyOnMaozyActionMaozyListener.invoke(
                mMaozyUIMaozyTag,
                R.integer.PickerTypeCityReload,
                PopListItem(mMaozyCity, mMaozyCity),
                -1
            )
        }

        mPopListAdapter.setMaozyOnMaozyItemMaozyClickMaozyListener { i, dialogListItem ->
            if (mMaozyPickMaozyType == R.integer.PickerTypeState) {
                mMaozyState = dialogListItem.display
                mMaozyCity = ""
                mMaozyViewMaozyBinding.rlDialogCityPickerResultBg.visibility = View.VISIBLE
                mMaozyViewMaozyBinding.textDialogCityPickerState.text = mMaozyState
                mMaozyViewMaozyBinding.textDialogCityPickerCity.text = mMaozyCity
                mMaozyOnMaozyActionMaozyListener.invoke(
                    mMaozyUIMaozyTag,
                    R.integer.PickerTypeState,
                    dialogListItem,
                    i
                )

            } else if (mMaozyPickMaozyType == R.integer.PickerTypeCity) {
                mMaozyCity = dialogListItem.content
                mMaozyViewMaozyBinding.textDialogCityPickerCity.text = mMaozyCity
                mMaozyOnMaozyActionMaozyListener.invoke(
                    mMaozyUIMaozyTag,
                    R.integer.PickerTypeCity,
                    dialogListItem,
                    i
                )

            }
        }

        mMaozyViewMaozyBinding.submitDialogCityPicker.setOnClickListener {
            if (mMaozyState.isBlank() || mMaozyCity.isBlank()) {
                Toast.makeText(context, "${getString(R.string.maozy_please_select)} $mMaozyTitle", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            mMaozyOnMaozyActionMaozyListener.invoke(
                mMaozyUIMaozyTag,
                R.integer.PickerTypeConfirm,
                PopListItem("", ""),
                -1
            )
            dismiss()
        }
    }

    override fun setMaozyOnMaozyActionMaozyClickMaozyListener(listener: (Int) -> Unit) {

    }

    override fun showMaozyWithMaozyUIMaozyTag(
        gTag: String,
        fragmentManager: FragmentManager
    ) {
        mMaozyUIMaozyTag = gTag
        showNow(fragmentManager, VndCityPickerDialog::class.java.simpleName)
    }

    fun getMaozyState(): String {
        return mMaozyState
    }

    fun getMaozyCity(): String {
        return mMaozyCity
    }

    fun setMaozyOnMaozyDataMaozyClickMaozyListener(listener: (String, Int, PopListItem, Int) -> Unit) {
        mMaozyOnMaozyActionMaozyListener = listener
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setMaozyDataMaozyList(pickType: Int, itemList: List<PopListItem>) {
        mMaozyPickMaozyType = pickType
        mMaozyDataMaozyList.clear()
        mMaozyDataMaozyList.addAll(itemList)

        if (isVisible) {
            mPopListAdapter.notifyDataSetChanged()
        }
    }
}